package WSS;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import Http.ConnectionString;
import Objetos.Sesion;
import Pantallas.VentanaInfo;

/**
 * @author ARMGARCES
 *
 */
public class WssUsuario {

	private static String serviceUrl;
	private static String serviceNamespace;
	private static String soapAction;

	
	/**
	 * 
	 * @param user
	 * @param pswd
	 * @return
	 */
	public static Sesion iniciarSesion(final String user, final String pswd){
		try{			
			serviceUrl 		= "http://digamaracay3.dynalias.com:8080/WSS/Usuarios?wsdl";
			serviceNamespace= "http://digamaracay3.dynalias.com:8080/WSS/Usuarios";
			soapAction 		= "http://digamaracay3.dynalias.com:8080/WSS/iniciarSesion";
			String methodName = "iniciarSesion";
			serviceUrl 		 += ConnectionString.getConnectionString();
			//
			SoapObject rpc = new SoapObject(serviceNamespace, methodName);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			
			// Definimos los valores de los parametros!!
			Sesion sesion = new Sesion(true);
			sesion.setUser(user);
			sesion.setPswd(pswd);	
			
			envelope.addMapping(serviceNamespace, "sesion", new Sesion().getClass());
			rpc.addProperty("sesion",sesion);
			
			envelope.bodyOut = rpc;
			envelope.dotNet = false;
			envelope.implicitTypes = true;
			envelope.encodingStyle = SoapSerializationEnvelope.ENC;
		
			HttpTransport ht = new HttpTransport(serviceUrl);
			ht.debug = true;
			ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
			//**************************************
			
        	ht.call(soapAction, envelope); 
        	if(envelope.getResponse() !=null){
        		SoapObject _soap = (SoapObject) envelope.getResponse();
	    		Sesion resp = new Sesion(_soap);
				return resp;
			}else{
				return null;
			}
		}catch(final Exception e){
			VentanaInfo.mostrarMensaje("Error: ", e.getMessage());			
		}
		return null;
	}
	
}
